package cn.edu.cqu.fredyvia.Utils;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;

import java.io.IOException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public class Util {
    public static boolean getMailType(String s) {
        return s.equals(Constants.MAIL_GOOD);
    }

    private static Path baseFolder = null;
    private static final Double division = 0.9;

    public static Path getBaseFolder() {
        return baseFolder;
    }

    public static void setBaseFolder(Path basePath) {
        baseFolder = basePath;
    }

    public static List<Path> getFolders(String parentFolder, Configuration conf) throws IOException {
        FileSystem hdfs = FileSystem.get(URI.create(parentFolder), conf);
        FileStatus[] fs = hdfs.listStatus(new Path(parentFolder));
        List<Path> paths = new ArrayList<Path>();
        for (FileStatus sta : fs) {
            if (sta.isDirectory()) {
                paths.addAll(getFolders(sta.getPath().toString(), conf));
            } else {
                paths.add(sta.getPath());
            }
        }
        return paths;
    }

    public static boolean checkString(String temp) {
        temp = temp.replaceAll("[^a-zA-Z0-9]", "");
        Pattern pattern = Pattern.compile("^-?[0-9]+"); //这个也行
        Matcher isNum = pattern.matcher(temp);
        return !isNum.matches() && temp.length() > 1;
    }

    public static String predoString(String temp) {
        temp = temp.replaceAll("[^a-zA-Z0-9]", "");
        return temp;
    }
//    String remove_StopWords_Punctunation(String s){
//
//    }
}